<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => '未登录']);
    exit;
}

$user_id = $_SESSION['user_id'];
$input = json_decode(file_get_contents('php://input'), true);
$deposit_id = intval($input['deposit_id'] ?? 0);

try {
    // 获取充值记录状态
    $stmt = $pdo->prepare("
        SELECT id, status, amount, admin_note, processed_at 
        FROM deposit_records 
        WHERE id = ? AND user_id = ?
    ");
    $stmt->execute([$deposit_id, $user_id]);
    $deposit = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$deposit) {
        throw new Exception('充值记录不存在');
    }
    
    $response = [
        'success' => true,
        'status' => $deposit['status'],
        'amount' => $deposit['amount'],
        'processed_at' => $deposit['processed_at'],
        'admin_note' => $deposit['admin_note']
    ];
    
    // 如果充值已完成，更新用户余额显示
    if ($deposit['status'] === 'completed') {
        $user_balance = getUserBalance($user_id);
        $response['new_balance'] = $user_balance;
    }
    
    echo json_encode($response);
    
} catch (Exception $e) {
    error_log("检查充值状态失败: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>
